/*!
    \file  main.c
    \brief USART printf

    \version 2019-6-5, V1.0.0, firmware for skylark
*/

/*
    Copyright (c) 2019, GigaDevice Semiconductor Inc.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/


#include <stdio.h>

// See LICENSE for license details.

#include <stdlib.h>
//#include "nuclei_libopt.h"

#include "../../../../SoC/anlogic/Board/sf1_eval/Include/nuclei_sdk_hal.h"

//define
#define DLM_TEST



void print_misa(void)
{
    CSR_MISA_Type misa_bits = (CSR_MISA_Type) __RV_CSR_READ(CSR_MISA);
    static char misa_chars[30];
    uint8_t index = 0;
    if (misa_bits.b.mxl == 1) {
        misa_chars[index++] = '3';
        misa_chars[index++] = '2';
    } else if (misa_bits.b.mxl == 2) {
        misa_chars[index++] = '6';
        misa_chars[index++] = '4';
    } else if (misa_bits.b.mxl == 3) {
        misa_chars[index++] = '1';
        misa_chars[index++] = '2';
        misa_chars[index++] = '8';
    }
    if (misa_bits.b.i) {
        misa_chars[index++] = 'I';
    }
    if (misa_bits.b.m) {
        misa_chars[index++] = 'M';
    }
    if (misa_bits.b.a) {
        misa_chars[index++] = 'A';
    }
    if (misa_bits.b.b) {
        misa_chars[index++] = 'B';
    }
    if (misa_bits.b.c) {
        misa_chars[index++] = 'C';
    }
    if (misa_bits.b.e) {
        misa_chars[index++] = 'E';
    }
    if (misa_bits.b.f) {
        misa_chars[index++] = 'F';
    }
    if (misa_bits.b.d) {
        misa_chars[index++] = 'D';
    }
    if (misa_bits.b.q) {
        misa_chars[index++] = 'Q';
    }
    if (misa_bits.b.h) {
        misa_chars[index++] = 'H';
    }
    if (misa_bits.b.j) {
        misa_chars[index++] = 'J';
    }
    if (misa_bits.b.l) {
        misa_chars[index++] = 'L';
    }
    if (misa_bits.b.n) {
        misa_chars[index++] = 'N';
    }
    if (misa_bits.b.s) {
        misa_chars[index++] = 'S';
    }
    if (misa_bits.b.p) {
        misa_chars[index++] = 'P';
    }
    if (misa_bits.b.t) {
        misa_chars[index++] = 'T';
    }
    if (misa_bits.b.u) {
        misa_chars[index++] = 'U';
    }
    if (misa_bits.b.x) {
        misa_chars[index++] = 'X';
    }

    misa_chars[index++] = '\0';

    printf("MISA: RV%s\r\n", misa_chars);
}


/*!
    \brief      main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    int i = 0 ;
    // __RV_CSR_SET(CSR_MCACHE_CTL, CSR_MCACHE_CTL_IE);
    // __RV_CSR_SET(CSR_MCACHE_CTL, CSR_MCACHE_CTL_DE);
//    printf("usart init has been done!\n\r");

#if 0
  //  srand(__get_rv_cycle()  | __get_rv_instret() | __RV_CSR_READ(CSR_MCYCLE));
  //  uint32_t rval = rand();
  //  rv_csr_t misa = __RV_CSR_READ(CSR_MISA);
  //  printf("MISA: 0x%lx\r\n", misa);
  //  print_misa();
//  MCU 
// 9 DSI1   up       psel2
// 7 DSC    |        psel0
// 8 DSI0   down     psel1
    

    REG32(0xE0000000 + 0x90000) = 0x00000001; 
    while ( !(REG32(0xE0000000 + 0x90000) == 0x20200601)){
     i++;
    }
    

  //write to register addr = 0x0c, value =  0x10120258
    //REG32(0xE0000000 + 0x7000c) = 0x10120258; 
    REG32(0xE0000000 + 0x70030) = 0x10120258; 
	while ( !(REG32(0xE0000000 + 0x70030) == 0x10120258)){
     i++;
    }

       REG32(0xE0000000 + 0x80000) = 0x00000001; 
    while ( !(REG32(0xE0000000 + 0x80000) == 0x20200601)){
      i++; 
    }
    
// test ILM 
    REG32(0x08000000) = 0x08000000;
    REG32(0x08000400) = 0x08000400;
    REG32(0x08000800) = 0x08000800;
    REG32(0x08000C00) = 0x08000C00;
    REG32(0x08001000) = 0x08001000;
    REG32(0x08001400) = 0x08001400;
    REG32(0x08001800) = 0x08001800;
    REG32(0x08001C00) = 0x08001C00;

 while ( !(REG32(0x08000000) == 0x08000000)){
     i++;
    }
while ( !(REG32(0x08000400) == 0x08000400)){
     i++;
    }
while ( !(REG32(0x08000800) == 0x08000800)){
     i++;
    }
while ( !(REG32(0x08000C00) == 0x08000C00)){
     i++;
    }
while ( !(REG32(0x08001000) == 0x08001000)){
     i++;
    }
while ( !(REG32(0x08001400) == 0x08001400)){
     i++;
    }
while ( !(REG32(0x08001800) == 0x08001800)){
     i++;
    }
while ( !(REG32(0x08001C00) == 0x08001C00)){
     i++;
    }

#endif

#ifdef DLM_TEST

 	 printf("DLM test start: \n\r");
// test DLM 
   //size: 0x20000000~0x20001000 used for .data/.bss section.
//    REG32(0x20001000) = 0x20001000;
//    REG32(0x20001400) = 0x20001400;
//    REG32(0x20001800) = 0x20001800;
//    REG32(0x20001C00) = 0x20001C00;

while ( !(REG32(0x20001000) == 0x12345678)){
     i++;
    }
//while ( !(REG32(0x20001400) == 0x12345678)){
//     i++;
//    }
//while ( !(REG32(0x20001800) == 0x12345678)){
//     i++;
//    }
//while ( !(REG32(0x20001C00) == 0x12345678)){
//     i++;
//    }
	if(i!=0)
	{
		printf("DLM test FAIL! \n\r");
	}
	else
	{
		printf("DLM test PASS! \n\r");
	}

#endif

    #ifdef CFG_SIMULATION
        pass_fail_simulation(1);
    #endif 

    while(1);

}


